package com.huatek.authority.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.huatek.authority.entity.EfwDataRole;
import com.huatek.authority.entity.EfwTargetFieldMap;
import com.huatek.authority.service.EfwTargetFieldService;
import com.huatek.framework.entity.FwDuty;
import com.huatek.framework.service.SystemService;
import com.huatek.framework.tag.CommonBean;
import com.huatek.framework.tag.CutPageBean;
import com.huatek.framework.util.CommonUtil;
import com.huatek.framework.util.QueryCondition;
import com.huatek.framework.util.Util;
import com.huatek.framework.validate.ValidateFieldLength;

/**
 * 目标字段的Controller.
 *
 * @author Talor Yang
 */
@Controller
@RequestMapping("/target.do")
@SessionAttributes(types = EfwTargetFieldMap.class)
public class EfwTargetFieldController {

	/**
	 * 目标字段Service.
	 */
	@Autowired
	public EfwTargetFieldService efwTargetFieldService;
	@Autowired
	private SystemService systemService;

	private static final String TARGET_FIELD_DELETE = "authority/entitypage/";

	/**
	 * 重定向至查询页面.
	 */
	private static final String REDIRECTQUERYPATH = "redirect:target.do?actionMethod=query";


	/**
	 * 数据权限分页查询方法.
	 *
	 * @param model
	 *            SpringMVC框架的Model对象
	 * @param request
	 *            请求对象
	 * @return 返回到数据权限列表页面
	 */
	@RequestMapping(params = "actionMethod=query")
	public String query(final Model model, final HttpServletRequest request) {
		List<CommonBean> paramterList = QueryCondition.getQueryCondition(
				request, EfwTargetFieldMap.class);
		CutPageBean pageBean = QueryCondition.getQueryPageBean(request);
		model.addAttribute("queryParams", paramterList);
		model.addAttribute("pageBean", efwTargetFieldService.getPageBean(
				pageBean, paramterList));
		return "page.target_field";
	}
	/**
	 * 跳转至框架页面并指定了中间内容页面的请求路径.
	 *
	 * @param model
	 *            SpringMVC框架的Model对象
	 * @return 返回框架页面
	 */
	@RequestMapping(params = "actionMethod=init", method = RequestMethod.GET)
	public String init(final Model model) {
		// 指定内容区域页面请求路径
		model.addAttribute("initPage", "target.do?actionMethod=query");
		// 返回至框架页面
		return "page.ops2.common";
	}
		/**
	 * 编辑.
	 *
	 * @param id
	 *            被修改的关联的数据权限的Id
	 * @param model
	 *            SpringMVC框架的Model对象
	 * @return 返回至数据权限修改页面
	 */

	/**
	 * 跳转到数据权限新增页面.
	 *
	 * @param model
	 *            SpringMVC框架的Model对象
	 * @return 返回到数据权限添加页面
	 */
	@RequestMapping(params = "actionMethod=add", method = RequestMethod.GET)
	public String add(final Model model) {
		// 创建数据权限对象
		model.addAttribute(new EfwTargetFieldMap());
		model.addAttribute("systemList", systemService.getAvailableFwSystem());
		return  "page.target_edit";
	}
	/**
	 * 数据权限添加.
	 *
	 * @param model
	 *            SpringMVC框架的Model对象
	 * @param dataRole
	 *            被添加的数据权限
	 * @param result
	 *            校验结果对象
	 * @param status
	 *            session状态
	 * @return 返回到新增页面
	 */
	@RequestMapping(params = "actionMethod=add", method = RequestMethod.POST)
	public String processAdd(final Model model,
			@ModelAttribute final EfwTargetFieldMap targetField,
			final BindingResult result, final SessionStatus status) {
		return this.processEdit(model, targetField, result, status);
	}
	@RequestMapping(params = "actionMethod=edit",method = RequestMethod.GET)
	public String edit(@RequestParam("queryId") Long id,Model model){
		model.addAttribute(efwTargetFieldService.getObjectById(id));
			return "page.target_edit";
		}
	/**
	 * 渠道保存.
	 *
	 * @param model
	 *            SpringMVC中Model的对象
	 * @param dataRole
	 *            被添加或者修改的数据权限对象
	 * @param result
	 *            校验结果对象
	 * @param status
	 *            session状态
	 * @return 当添加或者修改数据权限不成功时跳回到添加或者修改的页面，当添加成功时返回到数据权限的浏览页面
	 */
	@RequestMapping(params = "actionMethod=edit", method = RequestMethod.POST)
	public String processEdit(final Model model,
			@ModelAttribute final EfwTargetFieldMap targetField,
			final BindingResult result, final SessionStatus status) {
		validate(targetField, result);
		if (result.hasErrors()) {
			return "page.target_edit";
		}
		if (efwTargetFieldService.saveOrUpdateEfwTargetField(targetField)) {
			status.setComplete();
		} else {
			result.rejectValue("mapName", "error.check.duplicate",
					new Object[] { targetField.getMapName() }, "该名称已经存在！");
			return "page.target_edit";
		}
		return REDIRECTQUERYPATH;
	}

	/**
	 * 添加数据权限的校验.
	 *
	 * @param dataRole
	 *            数据权限EfwDataAuthority的对象
	 * @param errors
	 *            错误信息
	 */
	public static void validate(final EfwTargetFieldMap targetField, final Errors errors) {
		if (CommonUtil.isZeroLengthTrimString(targetField.getMapName())) {
			errors.rejectValue("mapName", "required", "required");
		}
		if (CommonUtil.isZeroLengthTrimString(targetField.getMapName())) {
			errors.rejectValue("targetClass", "required", "required");
		}
		if (CommonUtil.isZeroLengthTrimString(targetField.getMapName())) {
			errors.rejectValue("targetField", "required", "required");
		}
		ValidateFieldLength.validateMaxLength(errors, "mapName", 60,
				"maxLimitLength", "Enter overflow.");
		ValidateFieldLength.validateMaxLength(errors, "targetClass", 60,
				"maxLimitLength", "Enter overflow.");
		ValidateFieldLength.validateMaxLength(errors, "targetField", 120,
				"maxLimitLength", "Enter overflow.");
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(params = "actionMethod=delete",method = RequestMethod.POST)
	public String delete(@RequestParam("id") Long[] ids) {
		List<EfwTargetFieldMap> objects = efwTargetFieldService.findDatasByIds(ids);
		if(objects!=null&&objects.size()>0){
			efwTargetFieldService.deleteAll(objects);
		}
		return REDIRECTQUERYPATH;
	}

}
